<script lang="ts">

export default {
  data() {
    return {
      show: false,
      selection: []
    }
  },
  methods: {
    open() {
      this.show = true
    },
    close() {
      this.show = false
    },
    /**
     * 批量生成登录口令
     * @param selection 选中的员工
     */
    generateTokenBatch(selection) {
      if (selection.length === 0) {
        this.$message.warning("请选择要生成登录口令的员工");
        return;
      }


      // 询问是否生成登录口令
      this.$confirm(`是否生成登录口令，会将口令发送短信到选中员工的手机号`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        this.selection = [];
        for (let i = 0; i < selection.length; i++) {
          this.selection.push({
            realName: selection[i].realName,
            mobile: selection[i].mobile,
            idCard: selection[i].idCard,
            loginPwd: "",
            isFinish: false,
            errMsg: ""
          });
        }

        this.show = true;

        for (let i = 0; i < selection.length; i++) {

          await new Promise((resolve, reject) => {
            this.$kt.requestSecurity.send({
              uri: "/admin/emp/generateToken",
              method: "post",
              data: {
                empId: selection[i].id
              },
              stateSuccess: (res) => {
                this.selection[i].isFinish = true;
                this.selection[i].loginPwd = res.data?.loginPwd;
                resolve(res);
              },
              stateFail: (err) => {
                this.selection[i].isFinish = true;
                if(err.stateCode==="frequentOperation"){
                  this.selection[i].errMsg = "十分钟之内重复";
                }else{
                  this.selection[i].errMsg = err.errMsg;
                }
                resolve(err);
              }
            });
          });


        }

      });
    }
  }
}
</script>

<template>
  <el-dialog
      v-model="show"
      :title="'数量：'+selection.length"
      width="700px"
      @close="close">

      <el-table :data="selection"
      height="calc(100vh - 300px)"
      >
        <el-table-column
            width="100"
            prop="realName"
            label="姓名"
        ></el-table-column>
        <el-table-column
            width="200"
            prop="mobile"
            label="手机号"
        ></el-table-column>
        <el-table-column
            width="200"
            prop="idCard"
            label="身份证"
        ></el-table-column>
        <el-table-column
            width="200"
            label="登录口令">
          <template #default="{row}">
            <div v-if="!row?.isFinish">
              <span>生成中</span>
            </div>
            <div v-else>
              <div
                  class="tag"
                  v-if="row?.errMsg">{{ row?.errMsg }}</div>
              <span v-else>{{ row?.loginPwd }}</span>
            </div>
          </template>
        </el-table-column>
      </el-table>

  </el-dialog>
</template>

<style lang="scss" scoped>
.tag{
  color: #f7ba2a;
}
</style>